package com.yxhl.zoume.core.user.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.IdentificationView;
import com.yxhl.zoume.data.http.rest.param.identification.IdentificationParam;
import com.yxhl.zoume.data.http.rest.response.identification.IdentificationResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.indetification.IdentificationPostUseCase;
import com.yxhl.zoume.domain.interactor.indetification.IdentificationQueryUseCase;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/9/20 .
 */

public class IdentificationPresenter extends BasePresenter implements IPresenter {

    IdentificationView mIdentificationView;
    IdentificationQueryUseCase mIdentificationQueryUseCase;
    IdentificationPostUseCase mIdentificationPostUseCase;

    @Inject
    public IdentificationPresenter(Context context,
                                   IdentificationQueryUseCase identificationQueryUseCase,
                                   IdentificationPostUseCase identificationPostUseCase) {
        super(context);
        mIdentificationQueryUseCase = identificationQueryUseCase;
        mIdentificationPostUseCase = identificationPostUseCase;
    }

    public void QueryIDStatus(IdentificationParam param) {
        mIdentificationQueryUseCase.execute(param,new ZMSubscriber<IdentificationResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mIdentificationView.renderIDError(e);
                    }

                    @Override
                    public void onNext(IdentificationResponse identificationResponse) {
                        super.onNext(identificationResponse);
                        if (identificationResponse != null) {
                            boolean isSucc = identificationResponse.isSucc();
                            if (isSucc) {
                                mIdentificationView.renderIDSuccess(identificationResponse);
                            } else {
                                mIdentificationView.renderIDFailure(identificationResponse.getResultMessage());

                            }
                        }
                    }
                });

    }

    public void postIdCertify(IdentificationParam param) {
        mIdentificationPostUseCase.execute(param,new ZMSubscriber<IdentificationResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mIdentificationView.renderIDError(e);
                    }

                    @Override
                    public void onNext(IdentificationResponse identificationResponse) {
                        super.onNext(identificationResponse);
                        if (identificationResponse != null) {
                            boolean isSucc = identificationResponse.isSucc();
                            if (isSucc) {
                                mIdentificationView.renderIDSuccess(identificationResponse);
                            } else {
                                mIdentificationView.renderIDFailure(identificationResponse.getResultMessage());
                            }
                        }
                    }
                });
    }

    @Override
    public void attachView(BaseView view) {
        this.mIdentificationView = (IdentificationView) view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mIdentificationPostUseCase.unsubscribe();
        mIdentificationQueryUseCase.unsubscribe();
    }
}
